using System.Collections.Generic;
using UnityEngine;

public class TrackArrowController : MonoBehaviour
{
	public static TrackArrowController instance;

	public Transform curArrow;

	private InfoArrow curInfoArrow;

	public InfoTrack curTrack;

	public List<InfoArrow> arrows = new List<InfoArrow>();

	public List<InfoTrack> allTrack = new List<InfoTrack>();

	private Transform objTrackCamera;

	private Vector3 cashPosition;

	private Vector3 _pos;

	private Camera arrowCam;

	private void Awake()
	{
		instance = this;
		arrowCam = GetComponentInChildren<Camera>();
	}

	private void OnDestroy()
	{
		instance = null;
	}

	private void Start()
	{
		objTrackCamera = new GameObject("TrackArrow_Camera").transform;
		objTrackCamera.parent = GameController.instance.cameraGame.transform;
		objTrackCamera.localPosition = Vector3.zero;
		objTrackCamera.localRotation = Quaternion.identity;
	}

	private void LateUpdate()
	{
		if (curTrack != null)
		{
			if (!curTrack.unlimited)
			{
				curTrack.time -= Time.deltaTime;
				if (curTrack.time <= 0f)
				{
					DestroyCurTrack();
				}
				else
				{
					UpdateRotationArrow();
				}
			}
			else
			{
				UpdateRotationArrow();
			}
		}
		else if (curArrow != null)
		{
			DestroyArrow();
		}
	}

	public void UpdateRotationArrow()
	{
		if (curArrow != null && curTrack != null)
		{
			if (curTrack.trackTransform)
			{
				_pos = curTrack.objTrack.transform.position;
			}
			else
			{
				_pos = curTrack.positionTrack;
			}
			_pos.y = objTrackCamera.position.y;
			cashPosition.y = _pos.y;
			cashPosition = Vector3.Lerp(cashPosition, _pos, Time.deltaTime * 0.5f);
			objTrackCamera.LookAt(cashPosition);
			curArrow.localRotation = objTrackCamera.localRotation;
		}
	}

	public void AddTrack(Transform _parent, Transform _needObj, Vector3 _pos, bool _unlimited, float _time, TypeArrow _type, int _priority, bool _changeInfo = false, bool _trackTransform = true)
	{
		InfoTrack infoTrack = null;
		for (int i = 0; i < allTrack.Count; i++)
		{
			if (allTrack[i].objTrack == _needObj)
			{
				infoTrack = allTrack[i];
				break;
			}
		}
		if (infoTrack == null || !_changeInfo)
		{
			infoTrack = new InfoTrack();
			infoTrack.objTrack = _needObj;
			allTrack.Add(infoTrack);
		}
		infoTrack.fromObj = _parent;
		infoTrack.time = _time;
		infoTrack.type = _type;
		infoTrack.priority = _priority;
		infoTrack.unlimited = _unlimited;
		infoTrack.positionTrack = _pos;
		infoTrack.trackTransform = _trackTransform;
		OnUpdateTrack();
	}

	public void DestroyAllTrackParent(Transform _needObj, bool needUpdate = true)
	{
		BCWDebug.Log("DestroyAllTrackParent");
		for (int i = 0; i < allTrack.Count; i++)
		{
			if (allTrack[i].fromObj == _needObj)
			{
				allTrack.RemoveAt(i);
				i--;
			}
		}
		if (needUpdate)
		{
			OnUpdateTrack();
		}
	}

	public void OnUpdateTrack()
	{
		BCWDebug.Log("OnUpdateTrack");
		InfoTrack infoTrack = null;
		curTrack = null;
		int num = -1;
		for (int i = 0; i < allTrack.Count; i++)
		{
			if (infoTrack == null || num < allTrack[i].priority)
			{
				infoTrack = allTrack[i];
				num = infoTrack.priority;
			}
		}
		if (infoTrack != null && infoTrack.unlimited)
		{
			allTrack.Clear();
			allTrack.Add(infoTrack);
		}
		curTrack = infoTrack;
		UpdateTypeArrow();
	}

	public void UpdateTypeArrow()
	{
		if (curTrack != null)
		{
			if (curInfoArrow != null && curInfoArrow.type == curTrack.type)
			{
				return;
			}
			DestroyArrow();
			InfoArrow infoArrow = null;
			for (int i = 0; i < arrows.Count; i++)
			{
				if (arrows[i].type == curTrack.type)
				{
					infoArrow = arrows[i];
					break;
				}
			}
			if (curInfoArrow == null || curInfoArrow != infoArrow)
			{
				curInfoArrow = infoArrow;
				if (curInfoArrow != null)
				{
					GameObject gameObject = Object.Instantiate(curInfoArrow.objArrow);
					curArrow = gameObject.transform;
					curArrow.transform.parent = base.transform;
					curArrow.position = Vector3.zero;
					curArrow.localScale = Vector3.one;
				}
			}
		}
		else
		{
			curInfoArrow = null;
			DestroyArrow();
		}
	}

	public void SetActivCamera(bool val)
	{
		if (arrowCam != null)
		{
			arrowCam.enabled = val;
		}
	}

	public void DestroyCurTrack()
	{
		if (curTrack != null)
		{
			allTrack.Remove(curTrack);
			OnUpdateTrack();
		}
	}

	private void DestroyArrow()
	{
		if (curArrow != null)
		{
			Object.Destroy(curArrow.gameObject);
		}
	}
}
